<template>
  <div
    :id="viewerId"
    :class="[
      'supermap-container',
      { 'supermap-container-compare-rh': compare }
    ]"
  ></div>
</template>

<script>
import { setAction } from '@/utils/helper'

export default {
  name: 'MapView',
  props: {
    // 是否分屏显示
    compare: {
      type: Boolean,
      default: false
    },
    viewerId: {
      type: String,
      default: ''
    }
  },
  mounted() {
    // const obj = [6378137.0, 6378137.0, 6356752.3142451793]
    // Cesium.Ellipsoid.WGS84 = Object.freeze(
    //   new Cesium.Ellipsoid(obj[0], obj[1], obj[2])
    // )
    const viewer = new Cesium.Viewer(this.viewerId, {
      selectionIndicator: false,
      timeline: false,
      baseLayerPicker: false,
      // shadows: true,
      infoBox: false,
      // geocoder: true,  //查询
      // skyBox: false, // 关闭天空盒会一同关闭太阳，场景会变暗
      navigation: false,
      contextOptions: {
        maxDrawingBufferWidth: window.innerWidth,
        maxDrawingBufferHeight: window.innerHeight
      }
      // imageryProvider: new Cesium.ArcGisMapServerImageryProvider({
      //   // url: 'https://mt1.google.cn/vt/lyrs=s&hl=zh-CN&x={x}&y={y}&z={z}&s=Gali'
      //   url: 'https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'
      // })
      // contextOptions: {
      //   // fsaaLevel: 4, //fsaa硬件反走样；1表示不开反走样，值越大反走样越厉害，性能越低,最大不要超过8
      //   msaaLevel: 8, // maaa硬件反走样
      //   requestWebgl2: true // Webgl2.0版本，才支持
      // },
      // orderIndependentTranslucency: false// 关闭OIT
    })
    viewer.scene.skyAtmosphere.show = false
    viewer.scene.globe.enableLighting = true
    viewer.shadows = true
    viewer.terrainShadows = Cesium.ShadowMode.ENABLED
    viewer.scene.sun.show = true
    // viewer.scene.screenSpaceCameraController.maximumZoomDistance = 25000

    const utc = Cesium.JulianDate.fromDate(
      new Date(2020, 10, 1, 12)
    ) // UTC
    viewer.clock.currentTime = utc

    viewer.scene.globe.depthTestAgainstTerrain = false

    // 添加平行光源
    // const position = new Cesium.Cartesian3(-2180765.1825337997, 4379131.271389126, 4092630.162654784)
    // const position1 = new Cesium.Cartesian3(-2180878.0519406004, 4378948.067730668, 4092609.6775166253)
    // const directionalLightHorizontal = new Cesium.DirectionalLight(position1, {
    //   targetPosition: new Cesium.Cartesian3(-2180587.505824384, 4379172.4152414, 4092534.7949573714),
    //   intensity: 2
    // })
    // const directionalLightVertical = new Cesium.DirectionalLight(position, {
    //   intensity: 2
    // })
    // viewer.scene.addLightSource(directionalLightVertical)
    // viewer.scene.addLightSource(directionalLightHorizontal)

    // 场景亮度
    // const stages = viewer.scene.postProcessStages
    // viewer.scene.brightness =
    //   viewer.scene.brightness ||
    //   stages.add(
    //     Cesium.PostProcessStageLibrary.createBrightnessStage()
    //   )
    // viewer.scene.brightness.enabled = true
    // viewer.scene.brightness.uniforms.brightness =
    //   Number(1.3)

    // viewer.scene.colorCorrection
    viewer.scene.colorCorrection.show = true
    viewer.scene.colorCorrection.brightness = 1
    // console.log(
    //   'colorCollection',
    //   viewer.scene.colorCorrection
    // )

    // viewer.resolutionScale = 0.8
    // 移除默认底图影像
    // viewer.scene.imageryLayers._layers.splice(0, 1)
    const firstImagery = viewer.scene.imageryLayers.get(0)
    viewer.scene.imageryLayers.remove(firstImagery, true)
    viewer.scene.globe.baseColor =
      Cesium.Color.fromCssColorString('#193b51')
    setAction(this.viewerId, viewer, Cesium)

    // 测试添加底图和地形
    // viewer.scene.imageryLayers.addImageryProvider(
    //   new Cesium.SuperMapImageryProvider({
    //     url: 'http://chengyu.caupdwest.com:7000/iserver/services/map-ugcv5-YingXiang2020/rest/maps/%E5%BD%B1%E5%83%8F2020',
    //     isSct: true
    //   }),
    //   0
    // )
    // viewer.terrainProvider = new Cesium.CesiumTerrainProvider({
    //   url: 'http://chengyu.caupdwest.com:7000/iserver/services/3D-WeiLaiKeJiChengShuJuJianKu/rest/realspace/datas/A_%E5%9C%B0%E5%BD%A2%E7%BC%93%E5%AD%98',
    //   // requestWaterMask: true,
    //   // requestVertexNormals: true,
    //   isSct: true
    // })

    // if (viewer.geocoder) {
    // 请开发者自行到supermap online官网（http://www.supermapol.com/）申请key
    // viewer.geocoder.viewModel.geoKey = 'fvV2osxwuZWlY0wJb8FEb2i5'
    // viewer.geocoder.viewModel.key = 'fvV2osxwuZWlY0wJb8FEb2i5'
    // }
    // viewer.terrainProvider = new Cesium.ArcGISTiledElevationTerrainProvider({
    //   url: 'https://elevation3d.arcgis.com/arcgis/rest/services/WorldElevation3D/Terrain3D/ImageServer',
    //   requestWaterMask: true, // 开启法向量
    //   requestVertexNormals: true, // 开启水面特效
    //   isSct: false
    // })
    // viewer.terrainProvider = new Cesium.CesiumTerrainProvider({
    //   url: 'https://www.supermapol.com/realspace/services/3D-stk_terrain/rest/realspace/datas/info/data/path',
    //   requestWaterMask: true, // 开启法向量
    //   requestVertexNormals: true, // 开启水面特效
    //   isSct: false
    // })
    // viewer.terrainProvider = Cesium.createWorldTerrain()
    this.$emit('onload', {
      id: this.viewerId,
      viewer: viewer
    })
  },
  beforeDestroy() {
    // console.log('beforeDestroy mapView', this.viewerId)
    this.$emit('beforeDestroy', {
      id: this.viewerId
    })
  }
}
</script>

<style lang="scss" scoped>
.supermap-container {
  width: 100%;
  height: 100%;
  overflow: hidden;

  ::v-deep {
    .cesium-viewer-bottom {
      display: none;
    }
  }
}

/* 重写Cesium的css */

/**cesium按钮背景色*/
.cesium-button {
  background-color: #3f4854;
  color: #e6e6e6;
  fill: #e6e6e6;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
  line-height: 32px;
}

.cesium-viewer-geocoderContainer .cesium-geocoder-input {
  background-color: rgba(63, 72, 84, 0.7);
}

.cesium-viewer-geocoderContainer,
.cesium-geocoder-input:focus {
  background-color: rgba(63, 72, 84, 0.9);
}

.cesium-viewer-geocoderContainer .search-results {
  background-color: #3f4854;
}

.cesium-geocoder-searchButton {
  background-color: #3f4854;
}

.cesium-infoBox-title {
  background-color: #3f4854;
}

.cesium-infoBox {
  background: rgba(63, 72, 84, 0.9);
}

.cesium-toolbar-button img {
  height: 100%;
}

.cesium-performanceDisplay-defaultContainer {
  top: auto;
  bottom: 35px;
  right: 50px;
}

.cesium-performanceDisplay-ms,
.cesium-performanceDisplay-fps {
  color: #fff;
}

/**cesium工具栏位置*/
.cesium-viewer-toolbar {
  top: auto;
  left: auto;
  right: 24px;
  /* top: 120px; */
  bottom: 35px;
  z-index: 40;
  /* display: flex; */
}

.cesium-viewer-toolbar > .cesium-toolbar-button,
.cesium-navigationHelpButton-wrapper,
.cesium-viewer-geocoderContainer {
  margin-bottom: 5px;
  float: right;
  clear: both;
  text-align: center;
}

.cesium-baseLayerPicker-dropDown {
  bottom: 0;
  right: 40px;
  max-height: 700px;
  margin-bottom: 5px;
}

.cesium-navigation-help {
  top: auto;
  bottom: 0;
  right: 40px;
  transform-origin: right bottom;
}

.cesium-sceneModePicker-wrapper {
  width: auto;
}

.cesium-sceneModePicker-wrapper
  .cesium-sceneModePicker-dropDown-icon {
  float: left;
  margin: 0 3px;
}

.cesium-viewer-geocoderContainer .search-results {
  left: 0;
  right: 40px;
  width: auto;
  z-index: 9999;
}

.cesium-infoBox-title {
  background-color: #3f4854;
}

.cesium-infoBox {
  top: 50px;
  background: rgba(63, 72, 84, 0.9);
}

/**左下工具栏菜单*/
.toolbar-dropdown-menu-div {
  background: rgba(43, 44, 47, 0.8);
  border: 1px solid #2b2c2f;
  z-index: 991;
  position: absolute;
  right: 60px;
  bottom: 40px;
  display: none;
}

.toolbar-dropdown-menu {
  min-width: 110px;
  padding: 0;
}

.toolbar-dropdown-menu > li {
  padding: 0 3px;
  margin: 2px 0;
}

.toolbar-dropdown-menu > li > a {
  color: #edffff;
  display: block;
  padding: 4px 10px;
  clear: both;
  font-weight: normal;
  line-height: 1.6;
  white-space: nowrap;
  text-decoration: none;
}

.toolbar-dropdown-menu > li > a:hover,
.dropdown-menu > li > a:focus {
  color: #fff;
  background-color: #444d59;
}

.toolbar-dropdown-menu > .active > a,
.dropdown-menu > .active > a:hover,
.dropdown-menu > .active > a:focus {
  color: #fff;
  background-color: #444d59;
}

.toolbar-dropdown-menu i {
  padding-right: 5px;
}
</style>
